﻿Shader "Unlit/Water"
{
    Properties
    {
        _MainTex ("颜色贴图", 2d) = "White" {}
        _WarpTex ("扰动图", 2d) = "gary" {}
        _Speed ("X:流速x y:流速y ", vector) = (1.0, 1.0, 0.5, 1.0)
        _WarpParams1 ("X:大小 Y:流速x Z:流速y w:强度", vector) = (1.0, 1.0, 0.5, 1.0)
        _WarpParams2 ("X:大小 Y:流速x Z:流速y w:强度", vector) = (2.0, 0.5, 0.5, 1.0)
    }
    SubShader
    {
        Tags { 
            "Queue" = "Transparent"                     // 调整渲染顺序
            "RenderType"="TransparentCutout"            // 改为 cutout
            "ForceNoShadowCasting" = "True"             // 关闭阴影投射
            "IgnoreProjector" = "True"                  // 不响应投射器
            }
        LOD 100

        Pass
        {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }

            Blend One OneMinusSrcAlpha
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0

            // 输入参数
            uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
            uniform sampler2D _WarpTex;
            uniform half4 _Speed;
            uniform half4 _WarpParams1;
            uniform half4 _WarpParams2;  

            struct VertexInput {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv0 : TEXCOORD0;
                float2 uv1 : TEXCOORD1;
                float2 uv2 : TEXCOORD2;
            };

            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.pos = UnityObjectToClipPos( v.vertex );
                o.uv0 = v.uv - frac(_Time.x * _Speed);
                o.uv1 = v.uv * _WarpParams1.x - frac(_Time.x * _WarpParams1.yz);              // frac 取余
                o.uv2 = v.uv * _WarpParams2.x - frac(_Time.x * _WarpParams2.yz);              // frac 取余
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                // // 扰动遮罩
                // half warpMask = tex2D(_Mask , i.uv0).b;

                // 扰动
                half3 var_Warp1 = tex2D(_WarpTex, i.uv1).rgb;
                half3 var_Warp2 = tex2D(_WarpTex, i.uv2).rgb;

                // 扰动混合
                half2 warp = (var_Warp1.xy - 0.5) * _WarpParams1.w + (var_Warp2.xy - 0.5) * _WarpParams2.w;

                // 扰动UV
                float2 warpUV = i.uv0 + warp;

                // _MainTex 采样
                half3 var_MainTex = tex2D(_MainTex, warpUV);

                // half3 finalRGB = _ColorOut * var_Mask.r + _ColorIn * var_Mask.g;
                
                // half opacity = var_Mask.r + var_Mask.g;


                return half4(var_MainTex.xyz, 1.0);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}
